package xps.and.uudaijia.userclient.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;

import com.jude.utils.JUtils;

import java.util.List;

import rx.Subscriber;
import xps.and.uudaijia.userclient.adapter.YHJAdapterDaijia;
import xps.and.uudaijia.userclient.data.baen.YHJBean;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.ListDividerItemDecoration;
import xps.and.uudaijia.userclient.view.base.BaseSwipeRefreshFragment;
import xps.and.uudaijia.userclient.view.base.BaseSwipeRefreshFragmentYHJ;

public class YHJSYDaijiaFragment extends BaseSwipeRefreshFragmentYHJ {
    String userId;


    @Override
    protected void getData(int pageIndex) {
        int page = pageIndex + 1;
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        ListDividerItemDecoration list = new ListDividerItemDecoration((int) (1 * getActivity().getResources().getDisplayMetrics().density));
        recyclerView.addItemDecoration(list);
        setRefreshEnable(false);
        douserId();
        yhj();
        super.onActivityCreated(savedInstanceState);
    }

    void yhj() {

        abstractRecyclerAdapter = new YHJAdapterDaijia(activity, recyclerView);
        abstractRecyclerAdapter.setAutoLoadMoreEnable(true);
        abstractRecyclerAdapter.setOnItemClickListener((v, p) -> {
            YHJBean.ReturnBodyBean dataBean = (YHJBean.ReturnBodyBean) abstractRecyclerAdapter.getItem(p);
            String getCouponId = dataBean.getCouponId();
            String getCouponMoney = dataBean.getCouponMoney();
            Intent intent = new Intent("com.nangch.broadcasereceiver.MYRECEIVER");
            intent.putExtra("getCouponId", getCouponId);
            intent.putExtra("getCouponIdPD", "1");
            intent.putExtra("getCouponMoney", getCouponMoney);  //向广播接收器传递数据
            getActivity().sendBroadcast(intent);
            getActivity().finish();
        });

    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    void douserId() {
        userId = JUtils.getSharedPreference().getString("User_ID", "");
        UserNetWorks.getYHJ("1", new Subscriber<YHJBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(YHJBean yhjBean) {
                List<YHJBean.ReturnBodyBean> list = yhjBean.getReturn_body();
                if (list != null && list.size() <= 0) {
                    onFail("没有更多");
                    return;
                }
                adapterListData = list;
                onResponce();
            }
        });
    }
}
